package com.fruit.controller.wap.order;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import com.fruit.controller.base.BaseController;
import com.fruit.service.order.MailOrderManager;
import com.fruit.util.CommonUtil;
import com.fruit.util.ResultInfo;
import com.fruit.util.ResultUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * 寄回订单controller
 *
 * @author shibeilei
 * @version 2018.01.02 17:20
 */
@Slf4j
@Controller
@RequestMapping(value = "/wap/order/mail/")
public class MailOrderController extends BaseController {

    @Autowired
    private MailOrderManager mailOrderService;

    /**
     * 寄回订单列表 <br/>
     *
     * @param request
     * @param userId       用户ID
     * @param orderGoodsId 商品订单ID
     * @return 返回寄回订单列表
     */
    @RequestMapping(value = "list", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Map<String, Object> list(HttpServletRequest request, long orderGoodsId,
                                    @SessionAttribute("userId") int userId) {
        String tradeId = CommonUtil.getUUID();
        log.info("[{}]-【寄回订单列表】-【用户：{}】-【开始】", tradeId, userId);
        Map<String, Object> resMap = new HashMap<String, Object>();
        try {
            if (userId <= 0) {
                resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "user_not_exists", "用户不存在!"));
                return resMap;
            }

            if (orderGoodsId <= 0) {
                resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "order_not_exists", "订单不存在!"));
                return resMap;
            }

            Map<String, Object> params = new HashMap<String, Object>();
            params.put("orderGoodsId", orderGoodsId);
            params.put("userId", userId);
            resMap = mailOrderService.getMailOrderList(tradeId, params);
        } catch (Exception e) {
            e.printStackTrace();
            resMap.put("result", this.initResult(ResultInfo.ERROR, "系统繁忙", "系统繁忙"));
        }

        return resMap;

    }

    /**
     * 修改寄回订单 <br/>
     *
     * @param request
     * @param userId       用户ID
     * @param orderGoodsId 商品订单ID
     * @return 返回寄回订单列表
     */
    @RequestMapping(value = "updateMailOrderAddress", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Map<String, Object> updateMailOrderAddress(HttpServletRequest request, int addressId, long id) {
        String tradeId = CommonUtil.getUUID();
        log.info("[{}]-【寄回订单列表】-【用户：{}】-【开始】", tradeId, addressId, id);
        Map<String, Object> resMap = new HashMap<String, Object>();
        try {
            if (addressId <= 0) {
                resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "user_not_exists", "地址不存在!"));
                return resMap;
            }

            if (id <= 0) {
                resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "order_not_exists", "订单不存在!"));
                return resMap;
            }

            Map<String, Object> params = new HashMap<String, Object>();
            params.put("addressId", addressId);
            params.put("id", id);
            resMap = mailOrderService.updateMailOrderAddress(tradeId, params);
        } catch (Exception e) {
            e.printStackTrace();
            resMap.put("result", this.initResult(ResultInfo.ERROR, "系统繁忙", "系统繁忙"));
        }

        return resMap;

    }

    /**
     * 邮寄订单列表
     *
     * @return
     */
    @RequestMapping(value = "mailOrderInfo", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Map<String, Object> mailOrderInfo(long id) {
        log.info("已发货订单详情-开始");
        String tradeId = CommonUtil.getUUID();
        Map<String, Object> resMap = mailOrderService.getMailOrderInfo(tradeId, id);
        log.info("已发货订单列表-结束");
        return resMap;
    }

    /**
     * 返回可用物流
     *
     * @param baseId
     * @param addressId
     * @return
     */
    @RequestMapping(value = "getLogisticsDoList", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Map<String, Object> getLogisticsDoList(int baseId, int addressId) {
        Map<String, Object> result = new HashMap<String, Object>();
        /*result.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "OK", "查询成功"));
        return result;*/
        String tradeId = CommonUtil.getUUID();
        try {
        	log.info("[{}]返回可用物流-开始", tradeId);
            result = mailOrderService.getLogisticsDoList(tradeId, baseId, addressId);
            return result;
        } catch (Exception e) {
        	log.error("[{}]返回可用物流-异常", tradeId);
        	e.printStackTrace();
            result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "返回可用物流异常"));
            return result;
        }
    }
}
